﻿<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class quyen_Model extends Model {

    function __construct(){
        parent::__construct();
    }
    public function qry_all_quyen(){
        return $this->db->ExecuteCursor("BEGIN PHANQUYEN.SELECT_QUYEN(:CUR); END;",'CUR');
    }
    public function qry_all_quyen_filter($arr_filter)
    {
       /*
       Phan trang nằm ở function nó lấy cái biến từ:
       isset($_POST['sel_goto_page'])
       isset($_POST['sel_rows_per_page'])
       để tính ra  $P_START và $P_END
       */
        $P_MA_MENU = $arr_filter['cbx_MA_MENU_filter'];
       // echo '<script>alert("'.$P_STT_THU_TUC.'");</script>';
        return $this->db->ExecuteCursor("BEGIN PHANQUYEN.SELECT_QUYEN_FILTER
                                                      (:CUR,
                                                      '$P_MA_MENU'
                                                      );END;",'CUR');
    }
    public function qry_single_quyen($MA_QUYEN){
        return $this->db->ExecuteCursor("BEGIN PHANQUYEN.SELECT_QUYEN_DETAIL(:CUR,'$MA_QUYEN'); END;",'CUR');
    }
    public function update_quyen()
    {
        $P_UPDATE_TYPE        = get_post_var('hdn_update_type');
        $P_NGUOI_CAP_NHAT     = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_QUYEN           = get_post_var('txt_MA_QUYEN');
        $P_TEN_QUYEN          = get_post_var('txt_TEN_QUYEN');
        $P_MA_MENU            = get_post_var('cbx_MA_MENU');
        $P_APPS               = get_post_var('txt_APPS');
        $P_MODULES            = get_post_var('txt_MODULES');
        $P_HIEN_THI_TREN_MENU = get_post_var('cbx_HIEN_THI_TREN_MENU');
        $P_HIEN_THI_NHAC_VIEC = get_post_var('cbx_HIEN_THI_NHAC_VIEC');
        $P_STT_QUYEN          = get_post_var('txt_STT_QUYEN');
        $P_TRANG_THAI         = get_post_var('cbx_TRANG_THAI');

        $stmt = $this->db->PrepareSP("BEGIN PHANQUYEN.UPDATE_QUYEN(:P_VAL,:P_UPDATE_TYPE,:P_NGUOI_CAP_NHAT,:P_MA_QUYEN,:P_TEN_QUYEN,:P_MA_MENU,:P_APPS,:P_MODULES,:P_HIEN_THI_TREN_MENU,:P_HIEN_THI_NHAC_VIEC,:P_STT_QUYEN,:P_TRANG_THAI);END;");
        $this->db->OutParameter($stmt,$P_VAL,               'P_VAL');
        $this->db->InParameter($stmt, $P_UPDATE_TYPE,       'P_UPDATE_TYPE');
        $this->db->InParameter($stmt, $P_NGUOI_CAP_NHAT,    'P_NGUOI_CAP_NHAT');
        $this->db->InParameter($stmt, $P_MA_QUYEN,          'P_MA_QUYEN');
        $this->db->InParameter($stmt, $P_TEN_QUYEN,         'P_TEN_QUYEN');
        $this->db->InParameter($stmt, $P_MA_MENU,           'P_MA_MENU');
        $this->db->InParameter($stmt, $P_APPS,              'P_APPS');
        $this->db->InParameter($stmt, $P_MODULES,           'P_MODULES');
        $this->db->InParameter($stmt, $P_HIEN_THI_TREN_MENU,'P_HIEN_THI_TREN_MENU');
        $this->db->InParameter($stmt, $P_HIEN_THI_NHAC_VIEC,'P_HIEN_THI_NHAC_VIEC');
        $this->db->InParameter($stmt, $P_STT_QUYEN,         'P_STT_QUYEN');
        $this->db->InParameter($stmt, $P_TRANG_THAI,        'P_TRANG_THAI');
        $chaythutuc = $this->db->Execute($stmt);
        //echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->popup_exec_done();
          }elseif($P_VAL == 'TON_TAI_MA'){
              $this->popup_exec_fail(__('Mã quyền đã tồn tại!'));
              return;
          }elseif($P_VAL == 'TON_TAI_TEN'){
              $this->popup_exec_fail(__('Tên quyền đã tồn tại!'));
              return;
          }else{
               $this->popup_exec_fail(__('Không thể cập nhật!'));
               return;
          }
        }
    }
    public function delete_quyen(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_QUYEN = get_post_var('hdn_item_id',0);
        //echo '<script>alert("'.$P_MA_QUYEN.'");</script>';
        $stmt = $this->db->PrepareSP("BEGIN PHANQUYEN.DELETE_QUYEN(:P_VAL,:P_NGUOI_XOA,:P_MA_QUYEN);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_MA_QUYEN,'P_MA_QUYEN');
        $chaythutuc = $this->db->Execute($stmt);
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->exec_done($this->goback_url);
          }else{
               $this->popup_exec_fail(__('Không thể xóa!'));
               return;
          }
        }
    }
}